package com.itheima.service.system.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.ModuleDao;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.Module;
import com.itheima.domain.system.User;
import com.itheima.service.system.UserService;

import com.itheima.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;


import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private ModuleDao moduleDao;

    @Override
    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }

    @Override
    public void save(User user) {
        userDao.save(user);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public void update(User user) {
        /*user.setUserName(user.getUserName());
        user.setEmail(user.getEmail());
        user.setPassword(user.getPassword());
        user.setGender(user.getGender());
        user.setTelephone(user.getTelephone());
        user.setBirthday(user.getBirthday());*/
        userDao.update(user);
    }

    @Override
    public void delete(String id) {
        userDao.delete(id);
    }

    @Override
    public PageInfo findByPage(String companyId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userDao.findAll(companyId);
        return new PageInfo<>(list, 10);
    }

    @Override
    public List<String> findRoleIdsByUserId(String id) {
        return userDao.findRoleIdsByUserId(id);
    }

    @Override
    public void changeRole(String userId, String[] roleIds) {
        //1. 删除中间表中用户的现有角色对应的id
        userDao.deleteUserRoleByUserId(userId);

        //2. 重新向中间表插入新的用户和角色id
        if (roleIds != null && roleIds.length > 0) {
            for (String roleId : roleIds) {
                userDao.saveUserRole(userId, roleId);
            }
        }
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    @Override
    public List<Module> findModuleByUser(User user) {
        //然后根据身份对应的查找方式去查询模块信息
        Integer degree = user.getDegree();

        if (degree == 0) {//saas管理员
            return moduleDao.findByBelong(0);
        } else if (degree == 1) {// 企业管理员
            return moduleDao.findByBelong(1);
        } else { //普通员工
            return moduleDao.findByUserId(user.getId());
        }
    }


    @Value("wx3bdb1192c22883f3")
    private String appid;
    @Value("db9d6b88821df403e5ff11742e799105")
    private String secret;
    @Value("https://api.weixin.qq.com/sns/oauth2/access_token")

    private String accessTokenUrl;
    @Value("https://api.weixin.qq.com/sns/oauth2/refresh_token")
    private String wxInfoUrl;

    public User wxLogin(String code) {
        User user = null;
        //1.根据code获取access_token和openId
        String atUtl = accessTokenUrl + "?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
        Map<String, Object> map1 = HttpUtils.sendGet(atUtl);
        Object access_token = map1.get("access_token");
        Object openid = map1.get("openid").toString();
        //判断openid和access_token是否为空
        if (access_token == null && openid == null) {
            return user;
        }
        //2.根据openId判断用户是否存在
        user = userDao.findByOpenid(openid.toString());
        if (user != null) {
            System.out.println("返回数据库中的用户对象");
            //3.如果用户存在返回用户信息
            return user;
        } else {
           /* System.out.println("查询微信数据库");
            //4.如果用户不存在，根据access_token和openId获取微信用户信息

                       String wxurl = wxInfoUrl + "?access_token=" + access_token + "&openid=" + openid;

            Map<String, Object> map2 = HttpUtils.sendGet(wxurl);
            if (map2.isEmpty()) {
                return user;
            }
            */
            //5.将微信用户信息保存到数据库，返回用户数据
            user = new User();
            user.setOpenid(openid.toString());
            return user;
        }
    }

    @Override
    public User findByOpenid(String openid) {
        return userDao.findByOpenid(openid);
    }
}
